package com.oasistudio.ads.expert.banner;

import com.aiwan.AdListener;
import com.aiwan.AdManager;
import com.aiwan.AdView;
import com.oasistudio.ads.expert.R;

import android.app.Activity;
import android.graphics.Color;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class AdViewAiwan {
	public static final String UNION = "iaiwan.com";
	AiwanAdRequestListener adRequestListener;
	Activity activity;

	public AdViewAiwan(Activity activity) {
		this.activity = activity;
		this.adRequestListener = new AiwanAdRequestListener(activity, UNION);

		addAdwoAd();
	}
	
	/************************** www.iaiwan.com start *************************/
	private void addAdwoAd() {
		//初始化广告信息
		//第一个参数:context 
		//第二个参数:AppKey(注册后登录:SDK帮助中心>爱玩Key,如果没有,则AppKey就是你的登录账号) 
		//第三个参数:appName应用名字(您发布广告位后获得的appName 如下例子的musicplay)
		//第四个参数:背景颜色
		//第五个参数:背景颜色的透明度(0-25 5)
		//第六参数:广告文字的颜色
		//第七参数:您当前软件的版本号 如 1.2,由您自己定义 
		AdManager.initAd(activity, "mellon@sohu.com", "AdExpert", Color.GRAY, 100, Color.WHITE, "1.2");
		
		LinearLayout adContainer = (LinearLayout) activity.findViewById(R.id.ads_container);
		adContainer.addView(new TitleBuilder().createTitle(activity, UNION));

		LinearLayout aiwanContainer = new LinearLayout(activity);
		adContainer.addView(aiwanContainer);
		
		AdView mAdView = new AdView(activity);
		mAdView.setAdListener(this.adRequestListener);
		aiwanContainer.addView(mAdView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
	}
	/************************** www.iaiwan.com end *************************/

	class AiwanAdRequestListener extends AbstractAdsExpertAdListener implements AdListener {

		public AiwanAdRequestListener(Activity activity, String union) {
			super(activity, union);
		}

		@Override
		public void onFailedToReceiveAd(AdView arg0) {
			// TODO Auto-generated method stub
			this.fail();
		}

		@Override
		public void onFailedToRefreshAd(AdView arg0) {
			// TODO Auto-generated method stub
			this.fail();
		}

		@Override
		public void onReceiveAd(AdView adView) {
			// TODO Auto-generated method stub
			this.succeed();
			AdHistory.saveAd(UNION, adView);
		}

		@Override
		public void onRefreshAd(AdView adView) {
			// TODO Auto-generated method stub
			this.succeed();
			AdHistory.saveAd(UNION, adView);
		}
	}

}
